package com.googlecode.dtools.typeinfo.graphviz.holder.impl;

import com.googlecode.dtools.typeinfo.graphviz.bean.DmLink;
import com.googlecode.dtools.typeinfo.graphviz.format.Formatter;
import com.googlecode.dtools.typeinfo.graphviz.holder.DmLinkSetHolder;
import org.springframework.beans.factory.InitializingBean;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 *
 * @author A_Reshetnikov
 * @since Date: 22.05.12 Time: 17:21
 */
public class DmLinkSetHolderImpl implements DmLinkSetHolder, InitializingBean {
    private static Set<DmLink> set;
    private Formatter formatter;

    public void addAll(Collection<DmLink> col) {
        set.addAll(col);
    }

    public Set<DmLink> getAll() {
        return set;
    }

    public Set<DmLink> getAll(String typeName) {
        Set<DmLink> filteredSet = new HashSet<DmLink>();
        for (DmLink dmLink : set) {
            if (dmLink.getParent().equals(typeName)
                    || dmLink.getChild().equals(typeName)) {
                filteredSet.add(dmLink);
            }
        }
        return filteredSet;
    }

    public String getGraphvizText(String typeName) {
        Set<DmLink> col = getAll(typeName);
        if (col == null || col.size() == 0) return null;
        return formatter.format(typeName, getAll(typeName));
    }

    public void afterPropertiesSet() throws Exception {
        set = new HashSet<DmLink>();
    }

    public void setFormatter(Formatter formatter) {
        this.formatter = formatter;
    }
}
